package com.dev.bbs.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dev.bbs.domain.BbsUser;
import com.dev.bbs.domain.BbsUserInfo;
import com.dev.bbs.form.UserForm;
import com.dev.bbs.mapper.BbsUserMapper;
import com.dev.bbs.service.IBbsConfigService;
import com.dev.bbs.service.IBbsUserInfoService;
import com.dev.bbs.service.IBbsUserService;
import com.dev.common.constant.ConfigKeyConstants;
import com.dev.common.exception.BusinessException;
import com.dev.common.utils.RandomUtil;
import com.dev.common.utils.RedisUtils;
import com.dev.common.utils.SendMailUtils;
import com.dev.common.utils.StringUtils;
import com.dev.common.utils.bean.BeanUtils;

/**
 * 社区用户Service业务层处理
 *
 * @author ruoyi
 * @date 2019-09-17
 */
@Service
public class BbsUserServiceImpl implements IBbsUserService
{
    @Autowired
    private BbsUserMapper       bbsUserMapper;

    @Autowired
    private IBbsUserInfoService bbsUserInfoService;

    @Autowired
    private IBbsConfigService bbsConfigService;

    /**
     * 查询社区用户
     *
     * @param userId 社区用户ID
     * @return 社区用户
     */
    @Override
    public BbsUser selectBbsUserById(Long userId)
    {
        return bbsUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public BbsUser getUserWithInfoById(Long userId)
    {
        return bbsUserMapper.getUserWithInfoById(userId);
    }

    /**
     * 查询社区用户列表
     *
     * @param bbsUser 社区用户
     * @return 社区用户
     */
    @Override
    public List<BbsUser> selectBbsUserList(BbsUser bbsUser)
    {
        return bbsUserMapper.select(bbsUser);
    }

    /**
     * 新增社区用户
     *
     * @param bbsUser 社区用户
     * @return 结果
     */
    @Override
    public int insertBbsUser(BbsUser bbsUser)
    {
        if (bbsUserMapper.insertSelective(bbsUser) > 0)
        {
            return bbsUserInfoService.insertBbsUserInfo(new BbsUserInfo().setUserId(bbsUser.getUserId()));
        }
        return 0;
    }

    /**
     * 修改社区用户
     *
     * @param bbsUser 社区用户
     * @return 结果
     */
    @Override
    public int updateBbsUser(BbsUser bbsUser)
    {
        return bbsUserMapper.updateByPrimaryKeySelective(bbsUser);
    }

    /**
     * 删除社区用户对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBbsUserByIds(String ids)
    {
        return bbsUserMapper.deleteByIds(ids);
    }

    /**
     * 删除社区用户信息
     *
     * @param userId 社区用户ID
     * @return 结果
     */
    public int deleteBbsUserById(Integer userId)
    {
        return bbsUserMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public BbsUser selectUserByLoginName(String username)
    {
        return bbsUserMapper.selectOne(new BbsUser().setUsername(username));
    }

    /**
     * 校验注册用户是否唯一
     *
     * @param username
     * @return
     */
    @Override
    public boolean checkUsernameExist(String username)
    {
        return bbsUserMapper.checkUsernameExist(username) > 0;
    }

    @Override
    public boolean checkNicknameExist(String nickname)
    {
        return bbsUserMapper.checkNicknameExist(nickname) > 0;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.dev.bbs.service.IBbsUserService#updateUserInfo(com.dev.bbs.form.
     * UserForm)
     */
    @Override
    public int updateUserInfo(UserForm userForm, BbsUser bbsUser)
    {
        int index = 1;
        if (!bbsUser.getNickname().equals(userForm.getNickname()))
        {
            index = updateBbsUser(new BbsUser().setUserId(bbsUser.getUserId()).setNickname(userForm.getNickname()));
        }
        if (index != 1)
        {
            throw new RuntimeException("更新昵称错误");
        }
        BbsUserInfo userInfo = new BbsUserInfo();
        BeanUtils.copyBeanProp(userInfo, userForm);
        return bbsUserInfoService.updateBbsUserInfo(userInfo.setUserId(bbsUser.getUserId()));
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.dev.bbs.service.IBbsUserService#resetUserPwd(com.dev.bbs.domain.
     * BbsUser)
     */
    @Override
    public int resetUserPwd(BbsUser bbsUser)
    {
        return bbsUserMapper.resetUserPwd(bbsUser);
    }

    /**
     * 忘记密码---发送邮件
     * @param username
     */
    public void forgotPwdSendMail(String username){
        //校验是否一天发送三次了
        Object object = RedisUtils.get(username);
        Integer sendNum =object == null ? 0 : Integer.valueOf(object.toString());
        if(sendNum >= 3){
            throw new BusinessException("一天只能发送3次找回密码邮箱，请明天再来");
        }
        //校验该用户是否已注册
        BbsUser bbsUser = bbsUserMapper.selectOne(new BbsUser().setUsername(username));
        if(bbsUser == null){
            throw new BusinessException("该用户未注册！");
        }
        //校验用户邮箱是否正确
        String mail = bbsUser.getEmail();
        if(StringUtils.isEmpty(mail)){
            throw new BusinessException("该用户预留邮箱不正确，如需要继续找回密码，请联系社区管理员！");
        }
        if(!StringUtils.isEmail(mail)){
            throw new BusinessException("该用户预留邮箱不正确，如需要继续找回密码，请联系社区管理员！");
        }
        //生成找回密码---验证码---存储redis(15分钟)
        String code = RandomUtil.randomStr(64);
        RedisUtils.set(code,bbsUser.getUserId(),900);
        //发送邮箱----读取配置
        bbsConfigService.selectBbsConfigByKey(ConfigKeyConstants.FORGOT_MAIL);
        if(!SendMailUtils.sendForgotPwd(mail,code)){
            throw new BusinessException("发送失败");
        }
        //一天只能发送3次邮箱
        RedisUtils.set(username,sendNum + 1);
    }
}
